package douyin

import (
	"fmt"
	"gitee.com/edodo/flysdk/api"
)

const (
	API_HOST           = "https://developer.toutiao.com"
	SANDBOX_API_HOST   = "https://open-sandbox.douyin.com"
	ACCESS_TOKEN_URL   = "/api/apps/v2/token"
	JSCODE_SESSION_URL = "/api/apps/v2/jscode2session"
)

type DyError struct {
	ErrCode int64  `json:"err_no"`
	ErrMsg  string `json:"err_tips"`
}

type DyAccessToken struct {
	DyError
	Data api.AccessToken `json:"data"`
}

type DyCode2Session struct {
	DyError
	Data *api.Code2Session `json:"data"`
}

type WxPhoneNumber struct {
	DyError
	PhoneInfo *api.PhoneNumber `json:"phone_info"`
}

func (we DyError) IsOk() bool {
	return we.ErrCode == 0
}

func (we DyError) Err() error {
	return fmt.Errorf("err_code=%d, err_msg=%s", we.ErrCode, we.ErrMsg)
}

func (dat DyAccessToken) ToAccessToken() (at *api.AccessToken, e error) {
	if !dat.IsOk() {
		e = dat.Err()
		//glog.Errorf("Get access token failed: %v", e)
		return
	}
	at = &dat.Data
	at.Calc()
	return
}
